package pulpfizz.pulp;

import pulpcore.Stage;
import pulpcore.scene.Scene2D;
import pulpcore.sprite.FilledSprite;
import pulpcore.sprite.Group;
import pulpcore.sprite.Sprite;

public class FizzScene extends Scene2D
{
	private Group background;
	private PhysicsGroup canvas;
	private Group headsUpDisplay;

	private Camera camera;

	@Override
	public void load()
	{
		super.load();

		FilledSprite bgFill = new FilledSprite(0xFF000000);
		getMainLayer().add(bgFill);

		background = new Group();
		canvas = new PhysicsGroup(0,0,Stage.getWidth(),Stage.getHeight());
		headsUpDisplay = new Group();

		background.pixelSnapping.set(true);
		canvas.pixelSnapping.set(false);
		headsUpDisplay.pixelSnapping.set(true);
		
		addLayer(background);
		addLayer(canvas);
		addLayer(headsUpDisplay);
	}

	public void setCamera(Camera c)
	{
		this.camera = c;
		this.camera.setCanvasGroup(canvas);
	}

	public Camera getCamera()
	{
		return camera;
	}

	@Override
	public void update(int elapsedTime)
	{
		if (camera != null)
		{
			camera.update(elapsedTime);
		} else
		{
//			defaultTransform();
		}
		super.update(elapsedTime);
	}

	public void defaultTransform()
	{
		getCanvas().x.set(0);
		getCanvas().y.set(0);
		getCanvas().width.set(Stage.getWidth());
		getCanvas().height.set(Stage.getHeight());
	}

	@Override
	public synchronized void add(Sprite sprite)
	{
		getCanvas().add(sprite);
	}

	public static void addToCanvas(Sprite sprite)
	{
		getCanvas().add(sprite);
	}

	public static void addToBackground(Sprite sprite)
	{
		getBackground().add(sprite);
	}

	public static void addToHeadsUpDisplay(Sprite sprite)
	{
		getHeadsUpDisplay().add(sprite);
	}

	/**
	 * @return the background
	 */
	public static Group getBackground()
	{
		FizzScene fizz = (FizzScene) Stage.getScene();
		return fizz.background;
	}

	/**
	 * @return the canvas
	 */
	public static PhysicsGroup getCanvas()
	{
		FizzScene fizz = (FizzScene) Stage.getScene();
		return fizz.canvas;
	}

	/**
	 * @return the headsUpDisplay
	 */
	public static Group getHeadsUpDisplay()
	{
		FizzScene fizz = (FizzScene) Stage.getScene();
		return fizz.headsUpDisplay;
	}

	@Override
	public synchronized void remove(Sprite sprite)
	{
		getCanvas().remove(sprite);
	}

	public static void removeFromCanvas(Sprite sprite)
	{
		getCanvas().remove(sprite);
	}

	public static void removeFromBackground(Sprite sprite)
	{
		getBackground().remove(sprite);
	}

	public static void removeFromHeadsUpDisplay(Sprite sprite)
	{
		getHeadsUpDisplay().remove(sprite);
	}

	public static PhysicsPoint toPhysics(CanvasPoint point)
	{
		// Call canvas.converXXXX
		getCanvas();
		return null;
	}

	public static CanvasPoint toCanvas(PhysicsPoint point)
	{
		// Call canvas.convertXXX
		return null;
	}

	static FizzScene getScene()
	{
		return (FizzScene) Stage.getScene();
	}

}
